package com.hexorld.core;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;

import com.hexorld.utils.Constants;
import com.hexorld.utils.Coordinate;
import com.hexorld.utils.Position;

public abstract class Character extends HVisualObject {

	private int ID;
	protected Bitmap[] mTileBmp;
	protected int mCurTileIndex = 0;
	protected ObjectStatus mStatus;
	protected StatusView mStatusView;
	public int mType = -1;
	private Position mDstPosition = null;
	
	public Character(Context context, int width, int height, int id){
		super(context,width,height,new Position(0, 0));
		ID = id;
		loadRes();
		loadDefaultStatus();
	}
	
	protected void loadDefaultStatus(){
		mStatus = new ObjectStatus();
		mStatusView = new StatusView(mContext, mStatus);
		mStatusView.setPosition(this.position.x, this.position.y);
	}
	
	public void setStatus(ObjectStatus status){
		mStatus = status;
		mStatusView = new StatusView(mContext, mStatus);
		mStatusView.setPosition(this.position.x, this.position.y);
	}
	
	public ObjectStatus getObjectStatus(){
		return mStatus;
	}
	
	public StatusView getStatusView(){
		return mStatusView;
	}
	/*
	void test(int id){
		mTileBmp = new Bitmap[1];
		if(id == 0)
			mTileBmp[0] = Utils.getBitmapByResId(mContext, R.drawable.swordman);
		else
			mTileBmp[0] = Utils.getBitmapByResId(mContext, R.drawable.monster); 
		this.width = mTileBmp[0].getWidth();
		this.height = mTileBmp[0].getHeight();
	}
	*/
	protected abstract void loadRes();

	/**
	 * this point is in bottom-center of char
	 * different from map item
	 * so, re-caculate the rect for display
	 * @param p
	 */
	public void setPosition(Position p){
		this.position = p;
		coordinate =  Coordinate.createCoordinateByPos(this.position);
		if(mStatusView != null)
			mStatusView.setPosition(this.position.x, this.position.y);
	}
	
	public void setCoordinate(Coordinate c){
		this.coordinate = c;
		this.position = convert2Position(c);
		if(mStatusView != null)
			mStatusView.setPosition(this.position.x, this.position.y);
	}

	/**
	 * convert coordinate to position, bottom-center is in map-block 3/4bottom-center
	 * sub class can have different conversion formula
	 * @param c
	 * @return
	 */
	public Position convert2Position(Coordinate c){
		Position p = new Position(0,0);
		p.x = c.x * Constants.POS_TO_COOR_X + (Constants.POS_TO_COOR_X >> 1);
		p.y = c.y * Constants.POS_TO_COOR_Y + Constants.POS_TO_COOR_Y * 3/4; 
		return p;
	}
	
	public void draw(Canvas canvas) {
		if(canvas != null && mTileBmp[mCurTileIndex] != null)
		{
			prepareForDisplay();
			canvas.drawBitmap(mTileBmp[mCurTileIndex], null, mDstRect, mPaint);
		}
	}

	private void prepareForDisplay(){
		mDstRect.left = this.position.x - (this.width>>1);
		mDstRect.right = mDstRect.left + this.width;
		mDstRect.top = this.position.y - this.height;
		mDstRect.bottom = this.position.y;
	}
	
	public int getID(){
		return ID;
	}

	@Override
	public void update(long timeElapsed) {
		// TODO Auto-generated method stub
		doMove(timeElapsed);
	}

	private void doMove(long timeElapsed){
		if(mDstPosition.Equal(position))
			return;
		//TODO: MOVE ACTION
	}
	
	public void moveTo(int x, int y){
		if(mDstPosition == null)
			mDstPosition = new Position(x, y);
		else
		{
			mDstPosition.x = x;
			mDstPosition.y = y;
		}
	}
	
	public void moveBy(int dx, int dy){
		if(mDstPosition == null)
			mDstPosition = new Position(dx + position.x, dy + position.y);
		else
		{
			mDstPosition.x = dx + position.x;
			mDstPosition.y = dy + position.y;
		}
	}
}
